package util;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class ReliesUtil {
    public static void rels(ResultSet rs, Object o) throws SQLException, IllegalAccessException {
        Class<?> clazz = o.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            System.out.println(field.getName());
            System.out.println(rs.getString(field.getName()));
            field.setAccessible(true);
            String fieldName = field.getType().getTypeName();
            System.out.println(fieldName);
            if (null != rs.getObject(field.getName())) {
                if ("java.lang.Long".equals(fieldName)) {
                    Long l = Long.valueOf(rs.getObject(field.getName()).toString());
                    field.set(o, l);
                } else {
                    field.set(o, rs.getObject(field.getName()));
                }
            }
        }
    }

    /***
     * 将请求参数中的数据设置到对象属性中
     * @param req
     * @param o
     * @throws SQLException
     * @throws IllegalAccessException
     */
    public static void httuii(HttpServletRequest req, Object o) throws SQLException, IllegalAccessException {
        Class<?> clazz = o.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            String fieldName = field.getType().getTypeName();
            if (null != req.getParameter(field.getName())) {
                if ("java.lang.Long".equals(fieldName)) {
                    Long l = Long.valueOf(req.getParameter(field.getName()));
                    field.set(o, l);
                }
                if ("java.math.BigDecimal".equals(fieldName)) {
                    BigDecimal bd = new BigDecimal(req.getParameter(field.getName()));
                    field.set(o, bd);
                } else {
                    field.set(o, req.getParameter(field.getName()));
                }
            }
        }
    }
}
